package com.bw.zg2.view.fragment

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bw.zg2.R
import com.bw.zg2.base.BaseFragment
import com.bw.zg2.bean.CartBean
import com.bw.zg2.databinding.FragmentCartBinding
import com.bw.zg2.view.adapter.CartGoodsAdapter
import com.bw.zg2.viewmodel.CartViewModel
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter.OnItemClickListener
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * create an instance of this fragment.
 */
class CartFragment : BaseFragment<CartViewModel, FragmentCartBinding>(R.layout.fragment_cart) {
    lateinit var adapter: CartGoodsAdapter
    val data = mutableListOf<CartBean>()
    override fun initData() {
        viewModel.liveDataGoodsCart.observe(this) {
            if (it.data.isEmpty()) {
                dataBinding.tvEmpty.visibility = View.VISIBLE
                dataBinding.llContent.visibility = View.GONE

            } else {
                dataBinding.tvEmpty.visibility = View.GONE
                dataBinding.llContent.visibility = View.VISIBLE

                data.clear()
                data.addAll(it.data)

                adapter.notifyDataSetChanged()
            }
        }
    }

    override fun initView() {
        //获取购物车列表
        viewModel.getCarts()
        //初始化适配器
        adapter = CartGoodsAdapter(activity, R.layout.item_cart_goods_layout, data)
        dataBinding.rv.layoutManager = LinearLayoutManager(activity)
        dataBinding.rv.adapter = adapter

        adapter.setOnItemClickListener(object : OnItemClickListener {
            override fun onItemClick(p0: View?, p1: RecyclerView.ViewHolder?, p2: Int) {
            }

            override fun onItemLongClick(
                p0: View?,
                p1: RecyclerView.ViewHolder?,
                p2: Int
            ): Boolean {
                //删除一条数据
                data.removeAt(p2)
                //刷新适配器
                adapter.notifyDataSetChanged()
                //重新计算购物车数量和金额
//                count()
                return true
            }
        })

        dataBinding.checkAll.setOnClickListener {
            data.forEach {
                it.check = dataBinding.checkAll.isChecked
            }

            adapter.notifyDataSetChanged()
            //发送消息，通知页面选中效果
            EventBus.getDefault().post("cart")
        }
    }

    //接收EventBus消息
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun updateCheck(msg: String) {
        //被选中的总数
        var count = 0
        //被选中的总金额
        var priceTotal = 0
        //是否全选
        var isCheckAll = true
        //循环遍历被选中的商品
        data.forEach {
            if (it.check) {
                count += it.count
                //数量*单价
                priceTotal += it.count * it.goods_default_price
            } else {
                //不是全选
                isCheckAll = false
            }
        }

        //设置购物车数量
        dataBinding.tvCount.text = "结算 ($count)"

        dataBinding.tvPrice.text = "总金额：¥${priceTotal}"

        //如果选中数量等于 data的size，表示全部选中
         dataBinding.checkAll.isChecked = isCheckAll

//        if (count != data.size) {
//            dataBinding.checkAll.isChecked = false
//        } else {
//            dataBinding.checkAll.isChecked = true
//        }
    }

    override fun onStart() {
        super.onStart()
        EventBus.getDefault().register(this)
    }

    override fun onStop() {
        super.onStop()
        //取消注册，防止内存泄漏
        EventBus.getDefault().unregister(this)
    }
}